package com.example.testbank;

import java.util.Vector;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class TestRadio extends Fragment {
	
	public static final String ARG_QUESTION_NUMBER = "question_number";		// Common name used in the caller's class
	
	private RadioGroup radGroup;
    private RadioButton radButton;
    private TextView textView;
    private TextView answerText;
    private QSet qset;
	
    private View rootView;
    
    // Changed to Fragment
    // @Last Modified Justin
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		rootView = inflater.inflate(R.layout.activity_test_radio, container, false);
        int i = getArguments().getInt(ARG_QUESTION_NUMBER);
        qset = (QSet)getArguments().getSerializable("test");
        // Used to display image
        /*int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                        "drawable", getActivity().getPackageName());
        ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);*/              
		
		LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.radioTable);
		ll.setBackgroundColor(Color.BLACK);
        doInflateItems();
        //createQuestion();
        createUI();
        
        return rootView;
	}

	 private void doInflateItems() {
		 textView = (TextView) rootView.findViewById(R.id.questionText);
	     radGroup = (RadioGroup) rootView.findViewById(R.id.radGroup);
	     answerText = (TextView) rootView.findViewById(R.id.answerText);
	 }


	 private void createQuestion() {
		 String question = new String("Test Question");
		 String option1 = new String("No.1");
		 String option2 = new String("No.2");
		 String option3 = new String("No.3");
		 Vector<String> options = new Vector<String>();
		 options.add(option1);
		 options.add(option2);
		 options.add(option3);
		 String feedback = new String("Feedback");
		 int id = 1;
		 
		 Vector<Integer> arrange = new Vector<Integer>();
		 arrange.add(1);
		 arrange.add(2);
		 arrange.add(3);
			
		 qset = new QSet(question, options, arrange, feedback, id);
	 }
	 
	 private void createUI() {
		 textView.setTextColor(Color.WHITE);
		 textView.setText(qset.getQuestion());
		 
		 Vector<String> options = qset.getOptions();
		 Vector<Integer> randomOptions = qset.randomOptions();
		 for(int i = 0; i < options.size(); i++) {
			 radButton = new RadioButton(getActivity().getApplicationContext());
			 int currentOption = randomOptions.get(i);
	         radButton.setText(options.get(currentOption));
	         radGroup.addView(radButton);
	         radButton.setId(currentOption);
		 }
		 
		 radGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
		 {
		      public void onCheckedChanged(RadioGroup group, int checkedId) {
		    	  qset.chooseAnswer(checkedId);
		    	  answerText.setTextColor(Color.WHITE);
		    	  
		    	  if(checkedId == 0) {
		    		  answerText.setText(checkedId + " is the right answer");
		    	  } else {
		    		  answerText.setText(checkedId + " is the wrong answer");
		    	  }
		      }
		 });
	 }
	 
	/* Can add this back by calling setHasOptionsMenu(true) in onCreateView
	 * @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getActivity().getMenuInflater().inflate(R.menu.test_radio, menu);
		return true;
	}*/

}
